require("dotenv").config();
const nodemailer = require('nodemailer');
const express = require("express");
const https = require('https');
const cors = require("cors");
const bodyparser= require("body-parser");
const fs = require('fs');
const app = express();
app.use(cors());
app.use(bodyparser.json());
app.use(bodyparser.urlencoded({ extended: false }));
const port = 3002;
const prefer= 1.5;
const options = {
    key: fs.readFileSync('key.pem'),
    cert: fs.readFileSync('cert.pem')
};
v= true
v2= Date.now()

const transporter = nodemailer.createTransport({
    host: 'smtp.qq.com',
    port: 587,
    auth: {
        user: 'a',
        pass: 'a'
    }
});

 app.post("/", async (req, res) => {
    const { data } = req.body;
    if(data> prefer && Date.now()-v2> 300000){
        console.log("Sending email")
        await transporter.sendMail({
            from: 'from_address@example.com',
            to: '2907821379@qq.com',
            subject: data,
            text: '金价开始高于手续费'
        });
        v2= Date.now()
    }
    res.json({});
})

https.createServer(options, app).listen(port, function(req, res){
    console.log(`Https listening at https://localhost:${port}`);
});
